package fruitshopmall.Controller;


import fruitshopmall.Entity.Item;
import fruitshopmall.Entity.ItemCategory;
import fruitshopmall.Service.ItemCategoryService;
import fruitshopmall.Service.ItemService;
import fruitshopmall.base.BaseController;
import fruitshopmall.utils.Pager;
import fruitshopmall.utils.SystemContext;
import fruitshopmall.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/item")
public class ItemController extends BaseController {

    @Autowired
    private ItemService itemService;

    @Autowired
    private ItemCategoryService itemCategoryService;



    /**
     * 执行添加商品
     */
    @RequestMapping(value = "/exAdd")
    @ResponseBody
    public String exAdd(MultipartFile files){

        System.out.println(files);
        String fileName = files.getOriginalFilename();
        String filePath = "/resource/static/ueditor/upload/";

        System.out.println(fileName+filePath);
/*       itemCommon(item, files);
        item.setUrl1(dest);
        item.setGmNum(0);
        item.setIsDelete(0);
        item.setScNum(0);
        itemService.insert(item);*/
        return "success";
    }
    /**
     * 新增和更新的公共方法
     */
    private void itemCommon(Item item, @RequestParam("file") MultipartFile files) throws IOException {
               String n = UUIDUtils.create();
                String path = SystemContext.getRealPath() + "\\resource\\static\\ueditor\\upload\\" + n + files.getOriginalFilename();
                File newFile = new File(path);
                //通过CommonsMultipartFile的方法直接写文件static.ueditor.upload

                   // item.setUrl1(request.getContextPath()+"\\resource\\static\\ueditor\\upload\\" + n + files.getOriginalFilename());
                    item.setUrl1(item.getUrl1());



        ItemCategory byId = itemCategoryService.getById(item.getCategoryIdTwo());
        item.setCategoryIdOne(byId.getPid());
    }
    /**
     * 分页查询商品列表
     */
    @RequestMapping("/findBySql")
    public String findBySql( Item item){
        String sql = "select * from item where isDelete = 0 ";
        if(!isEmpty(item.getName())){
            sql += " and name like '%" + item.getName() + "%' ";
        }
        sql += " order by id desc";
        Pager<Item> pagers = itemService.findBySqlRerturnEntity(sql);

        return "item/item";
    }

}
